package com.qin.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qin.mall.common.api.CommonResult;
import com.qin.mall.common.api.MyBatisPlusPage;
import com.qin.mall.domain.PmsProductAttribute;
import com.qin.mall.domain.PmsProductCategoryAttributeRelation;
import com.qin.mall.dto.ProductAttributeParam;
import com.qin.mall.service.PmsProductAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品属性Controller
 * @author Leisure
 * create 2022 2022/11/6 9:38
 **/
@RestController
@RequestMapping("/productAttribute")
public class ProductAttributeController {
    
    @Autowired
    private PmsProductAttributeService productAttributeService;

    // 据分类分页获取商品属性
    @GetMapping("/list$/{cid}") 
    public CommonResult<MyBatisPlusPage<PmsProductAttribute>> getListByType(@PathVariable Long cid,
                                                                            @RequestParam(value = "type") Integer type,
                                                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        Page<PmsProductAttribute> products = productAttributeService.getList(cid, type, pageNum, pageSize);
        return CommonResult.success(MyBatisPlusPage.mpPage(products));
        
    }
    
    // 添加商品属性
    @PostMapping("/create$")
    public CommonResult createAttribute(@RequestBody ProductAttributeParam productAttributeParam){
        int count = productAttributeService.create(productAttributeParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    //修改商品属性
    @PostMapping("/update$/{id}")
    public CommonResult updateAttribute(@PathVariable Long id, @RequestBody ProductAttributeParam productAttributeParam){
        int count = productAttributeService.update(id, productAttributeParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    
    // 获取单个商品属性信息
    @GetMapping("$/{id}")
    public CommonResult<PmsProductAttribute> getAttributeById(@PathVariable Long id){
        PmsProductAttribute product = productAttributeService.getItem(id);
        return CommonResult.success(product);
    }
    
    //删除多个商品属性值
    @PostMapping("/delete$")
    public CommonResult deleteAttributeByIds(@RequestBody List<Long> ids){
        int count = productAttributeService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    //TODO 这里可能存在问题 根据分类获取商品属性信息
    @GetMapping("/attrInfo$/{productCategoryId}")
    public CommonResult<List<PmsProductCategoryAttributeRelation>> getProductAttrInfo(@PathVariable Long productCategoryId){
        List<PmsProductCategoryAttributeRelation> info = 
                productAttributeService.getProductAttrInfo(productCategoryId);
        return CommonResult.success(info);
    }


    
    
}
